<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<%@ include file="../bui_header.jsp" %>

<body>

<div class="container">
    <div class="row">
      <form id="searchForm" class="form-horizontal span24">
        <div class="row">
          <div class="control-group span6">
            <label class="control-label control-label-auto">公司名称：</label>
            <div class="controls">
              <input type="text" class="text" name="company_name">
            </div>
          </div>
          <div class="span4">
            <button type="button" id="btnSearch" class="button button-small button-primary">
            	<i class="icon-search icon-white"></i>查找
            </button>
          </div>
        </div>
        <div class="row">
        	<div class="control-group span8">
        		<div id="importDiv"></div>
        	</div>
        </div>
      </form>
    </div>
    <div class="search-grid-container">
      <div id="grid"></div>
    </div>

  </div>
  <div id="content" class="hide" disabled='true'>
      <form id="detail_form" class="form-horizontal">
        <!-- <input type="hidden" name="a" value="3"> -->
        <!-- <div class="row">
          <div class="control-group span8">
            <label class="control-label"><s>*</s>编号</label>
            <div class="controls">
              <input name="number" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div> -->
        <div class="row">
          <div class="control-group span8">
            <label class="control-label"><s>*</s>企业名称</label>
            <div class="controls">
              <input name="companyName" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->注册号</label>
            <div class="controls">
              <input name="registerNumber" type="text"  class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->法定代表人姓名</label>
            <div class="controls">
               <input name="delegatePeople"  type="text" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->企业类型</label>
            <div class="controls">
              <input name="companyType" type="text"  class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->住所/地址</label>
            <div class="controls">
              <input name="address" type="text"  class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->注册地址</label>
            <div class="controls">
              <input name="registerAddress"  type="text" class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->注册地</label>
            <div class="controls">
              <input name="registerLocation"  type="text" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->注册资金</label>
            <div class="controls">
              <input name="registerFund"  type="text" class="input-normal control-text">
            </div>
          </div>
         </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->注册币种</label>
            <div class="controls">
              <input name="registerCurrency"  type="text" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><!-- <s>*</s> -->成立日期</label>
            <div class="controls">
              <input name="establishDate" type="text"  class="calendar">
            </div>
          </div>
        </div>
      </form>
    </div>
    
    <div class="container hide">
	    <div class="row">
		    <div class="span24">
		      <div id="importFileDiv"></div>
		    </div>
	  </div>
	</div>
	  
<script type="text/javascript">
    BUI.use('common/page');
</script>
  
<script type="text/javascript">
BUI.use(['common/search', 'bui/overlay', 'bui/uploader', 'bui/form'], function (Search, Overlay, Uploader, Form) {
    
    //var enumObj = {"1":"男","0":"女"};
    var editing = new BUI.Grid.Plugins.DialogEditing({
        contentId : 'content', //设置隐藏的Dialog内容
        autoSave : false //添加数据或者修改数据时，不自动保存
        //triggerCls : 'btn-edit'
      });

    var columns = [
        /* {title:'编号',dataIndex:'number',width:'10%'}, */
        {title:'企业名称', dataIndex:'companyName', width:'20%'},
        {title:'注册号', dataIndex:'registerNumber',width:'12%'},
        {title:'法定代表人名称',dataIndex:'delegatePeople',width:'5%'},
        {title:'企业类型', dataIndex:'companyType',width:'20%'},
        /* {title:'行业类别',dataIndex:'businessType',width:'10%'}, */
        {title:'住所/地址',dataIndex:'address',width:'25%'},
        {title: '操作', width: '10%', renderer: function(value, obj) {
      	  //return '编辑';
      	  //var authBtn = Search.createLink({
         //     id : 'authSetting' + obj.id,
         //     title : '用户权限设置',
         //     text : '权限设置',
          //    href : '<%=contextPath %>/baseinfo/xedkgs/userAuthSetting.do?userId=' + obj.id
         //   });
      	  	var editBtn = '<span class="grid-command btn-edit">编辑</span>';
      	  	var viewBtn = '<span class="grid-command btn-view">查看</span>';
      		//var viewBtn =  '<span class="grid-command">' +
      			//	'<a href="javascript:viewRecord()">查看</a>' + '</span>';
      	  	return viewBtn + editBtn;
        }}
      ];
      var store = Search.createStore('<%=contextPath %>/baseinfo/xedkgs/getdata.do', {
    	  autoSync: true,
    	  proxy: {
      		method: "post",
      		dataType : 'json',
      		//handler: 
      		save : { //也可以是一个字符串，那么增删改，都会往那么路径提交数据，同时附加参数saveType
				addUrl : '<%=contextPath %>/baseinfo/xedkgs/editBaseInfo.do?action=add',
				updateUrl : '<%=contextPath %>/baseinfo/xedkgs/editBaseInfo.do?action=update',
				removeUrl : '<%=contextPath %>/baseinfo/xedkgs/editBaseInfo.do?action=remove'
         	}
    	  }
      });
     // initSearchStore(store);
/*       store.on("saved", function(ret){
    	 alert(JSON.stringify(ret.data)); 
      }); */
      var height = $(window).height() - 70;
      var gridCfg = Search.createGridCfg(columns, {
    	  height: height,
        tbar : {
          items : [
            {text : '<i class="icon-plus"></i>新建',btnCls : 'button button-small',handler: addFunction},
            {text : '<i class="icon-remove"></i>删除',btnCls : 'button button-small',handler : delFunction},
            {text : '<i class="icon-upload icon-white"></i>导入',btnCls : 'button button-small button-primary', handler: importData}
          ]
        },
        plugins : [BUI.Grid.Plugins.CheckSelection,editing] // 插件形式引入多选表格
      });

    var search = new Search({
        store : store,
        gridCfg : gridCfg
      });
    var grid = search.get('grid');
    // 当使用editing查看明细时必须调用该方法
   // initEditing(editing);
    initComponent(search, editing, store);
ss();
    
    function ss() {
    	var $grid = $(".bui-grid");
    	if (!$grid)
  		  return ;
    	var $gridBody = $grid.find(".bui-grid-body");
    	if (!$gridBody)
  		  return ;
    	$gridBody.css('overflow-y', 'auto');
	  
	  var $gridTable = $grid.find(".bui-grid-table");
	  if ($gridTable)
		  $gridTable.width($gridBody.width()); 
    }
    
    // 新增操作
    function addFunction(){
        //var newData = {isNew : true}; //标志是新增加的记录
        editing.add({}); //添加记录后，直接编辑
    }
    
    // 删除操作
    function delFunction(){
      	var selections = grid.getSelection();
     	delItems(selections);
    }
    
    // 导入操作
    function importData() {
    	var uploader = new Uploader.Uploader({
   	      	render: '#importFileDiv',
   	      	url: '<%=contextPath %>/baseinfo/xedkgs/importData.do',
   	      	autoUpload: false,
   	      	multiple: false,
   	      	text: '选择导入文件',
   	     	width: 360,
   	     	rules: {
   	     	 // maxSize: [1024, '文件最大不能超过1M!'],
   	     	 // minSize: [1, '文件最小不能小于1k!'],
   	     	  max: [1, '文件最多不能超过{0}个！'],
   	     	 // min: [1, '文件最少不能少于{0}个!'],
   	     	  ext: ['.xls', '文件类型只能为{0}']
   	     	},
   	     	isSuccess: function(result) {
   	     		if (result && result.success) {
		   	     	BUI.Message.Alert('导入数据成功！', function(){
		   	     		importDialog.close();
		   	     		loadGridData();
		   	        }, 'success');
   	     		} else {
   	     			BUI.Message.Alert(result.error, function(){
		   	          //alert('确认');
		   	        }, 'error');
   	     		}
   	     	}
   	    }).render();
    	 
    	var importDialog = new Overlay.Dialog({
            title: '导入文件',
            width: 400,
            height: 200,
            contentId: 'importFileDiv',
            success:function () {
              //alert('确认');
              //this.close();
              uploader.upload();
            }
          });
    	importDialog.show();
    }

    function delItems(items){
      var ids = [];
      BUI.each(items,function(item){
    	  if (item.number)
        	ids.push(item.number);
      });

      if(ids.length){
        BUI.Message.Confirm('确认要删除选中的记录么？',function(){
        	$.post('<%=contextPath %>/baseinfo/xedkgs/editBaseInfo.do?action=remove', {ids: ids}, function(ret) {
        		if(ret.success){ //删除成功
                	search.load();
                	BUI.Message.Alert('删除成功！');
                } else { //删除失败
                    BUI.Message.Alert('删除失败！');
                }
        	}, 'json');
        },'question');
      }
    }
    
    function loadGridData() {
    	search.load();
    }

    //监听事件，删除一条记录
     grid.on('cellclick', function(ev) {
     	var sender = $(ev.domTarget);
        var editor = editing.get('editor');
        var contain = false;
		if(sender.hasClass('btn-edit') && editor){
			contain = true;
			editing.edit(ev.record);
		}
        if (sender.hasClass('btn-view') && editor) {
        	contain = true;
      		var record = ev.record;
      		var view = {"view": true};
      		record.trigger_action = "view";
      		editing.showEditor(record);
      		setEditingEnable(editing, false);
      	}
        if (contain && grid.get('multipleSelect')){
		    return false;
		}
    });
  });

</script>

		
</body>
</html>